﻿LOAD_STRADE_VERSION = "$$$ sTrade v0.5.1  已加载 $$$"

--一些提示字符
sTrade_LOC_a1="Trade Error"
sTrade_LOC_a2="Trade Cancelled"
sTrade_LOC_a2="Trade Complete"
sTrade_LOC_b1=" at "
sTrade_LOC_b2=" with "
sTrade_LOC_b3=" trade list: "
sTrade_LOC_b4=" Result: "
sTrade_LOC_b5=" Reason: "
sTrade_LOC_c1=" Gold "
sTrade_LOC_c2=" Silver "
sTrade_LOC_c3=" Copper "

if GetLocale() == "zhCN" then
	sTrade_LOC_a1="交易错误"
	sTrade_LOC_a2="交易取消"
	sTrade_LOC_a3="交易完成"
	sTrade_LOC_b1=" 在 "
	sTrade_LOC_b2=" 与 "
	sTrade_LOC_b3=" 的交易: "
	sTrade_LOC_b4=" 交易结果: "
	sTrade_LOC_b5=" 原因: "
	sTrade_LOC_c1="金"
	sTrade_LOC_c2="银"
	sTrade_LOC_c3="铜"
end

sTradeLOG = {};
local sTradeLOGa="序号"
local sTradeLOGb="时间"
local sTradeLOGc="地点"
local sTradeLOGd="姓名"
local sTradeLOGe="我方"
local sTradeLOGf="对方"
local sTradeLOGg="结果"
local sTradePlayerPay;
local sTradeTargetPay;

local function curr()
	if(not CURRENT_sTRADE) then
		CURRENT_sTRADE = sTrade_NewTrade();
	end
	return CURRENT_sTRADE;
end

function sTrade_OnLoad()
	this:RegisterEvent("TRADE_SHOW");
	this:RegisterEvent("TRADE_CLOSED");
	this:RegisterEvent("TRADE_REQUEST_CANCEL");
	this:RegisterEvent("PLAYER_TRADE_MONEY");
	this:RegisterEvent("TRADE_MONEY_CHANGED");
	this:RegisterEvent("TRADE_PLAYER_ITEM_CHANGED"); --this is an uncertain problem, seems TRADE_PLAYER_ITEM_CHANGED always fire 2 times?
	this:RegisterEvent("TRADE_TARGET_ITEM_CHANGED");
	this:RegisterEvent("TRADE_ACCEPT_UPDATE");
	this:RegisterEvent("PLAYER_TRADE_MONEY");
	this:RegisterEvent("UI_INFO_MESSAGE");
	this:RegisterEvent("UI_ERROR_MESSAGE");
	this:RegisterEvent("PLAYER_ENTERING_WORLD");

	SlashCmdList["sTrade"] = sTrade_SlashHandler;
	SLASH_sTrade1 = "/stg";
	DEFAULT_CHAT_FRAME:AddMessage(LOAD_STRADE_VERSION,true);
	DEFAULT_CHAT_FRAME:AddMessage("$$$ 输入/stg 显示金币交易记录 $$$",0.5,0,1);

--窗体视图
	local f,sframe,t,i,j
	f, _, _ = GameFontNormal:GetFont()
--主窗体
	sTradeLLFrame = {}
	sTradeLLFrame.main = CreateFrame("Frame", "sTradeLLMainFrame", UIParent);
	sTradeLLFrame.main:Hide();
	sTradeLLFrame.main:SetWidth(740);
	sTradeLLFrame.main:SetHeight(400);
	sTradeLLFrame.main:SetPoint("CENTER", 0, 0);
	sTradeLLFrame.main:SetMovable(true);
	sTradeLLFrame.main:EnableMouse(true);
	sTradeLLFrame.main:SetBackdrop({
		bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background", tile = true, tileSize = 16,
		edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
		edgeSize = 16,
		insets = {left = 4, right = 4, top = 4, bottom = 4},
		});
	sTradeLLFrame.main:SetBackdropColor(0.75, 0.75, 0.75, 0.36);
	sTradeLLFrame.main:SetBackdropBorderColor(1, 1, 1, 1);
	sTradeLLFrame.main:SetScript("OnMouseDown",function()
	  sTradeLLFrame.main:StartMoving();end)
	sTradeLLFrame.main:SetScript("OnMouseUp",function()
	  sTradeLLFrame.main:StopMovingOrSizing();end)
--标题栏
	sTradeLLFrame.header = sTradeLLFrame.main:CreateFontString(nil,"OVERLAY")
	sTradeLLFrame.header:SetFont(f,14)
	sTradeLLFrame.header:SetWidth(600)
	sTradeLLFrame.header:SetText("sTrade 金币交易清单 ( 显示本页/stg )")
	sTradeLLFrame.header:SetTextColor(1, .8, 0)
	sTradeLLFrame.header:ClearAllPoints()
	sTradeLLFrame.header:SetPoint("TOP", sTradeLLFrame.main, "TOP", 0, -10)
--底部栏
	sTradeLLFrame.bottoma = sTradeLLFrame.main:CreateFontString(nil,"OVERLAY")
	  sTradeLLRR(sTradeLLFrame.bottoma,f,390,16,"小计",sTradeLLFrame.main,20,-370)
	  sTradeLLFrame.bottoma:SetTextColor(1, .8, 0)
	  sTradeLLFrame.bottoma:SetJustifyH("CENTER")
	sTradeLLFrame.bottomb = sTradeLLFrame.main:CreateFontString(nil,"OVERLAY")
	  sTradeLLRR(sTradeLLFrame.bottomb,f,120,16,"",sTradeLLFrame.main,410,-370)
	  sTradeLLFrame.bottomb:SetTextColor(1, .8, 0)
	sTradeLLFrame.bottomc = sTradeLLFrame.main:CreateFontString(nil,"OVERLAY")
	  sTradeLLRR(sTradeLLFrame.bottomc,f,120,16,"",sTradeLLFrame.main,530,-370)
	  sTradeLLFrame.bottomc:SetTextColor(1, .8, 0)
--各列
	sTradeLLFrame.titlesa = sTradeLLFrame.main:CreateFontString(nil,"OVERLAY")
	  sTradeLLRR(sTradeLLFrame.titlesa,f,40,16,"序号",sTradeLLFrame.main,20,-25)
	sTradeLLFrame.titlesb = sTradeLLFrame.main:CreateFontString(nil,"OVERLAY")
	  sTradeLLRR(sTradeLLFrame.titlesb,f,120,16,"时间",sTradeLLFrame.main,60,-25)
	sTradeLLFrame.titlesc = sTradeLLFrame.main:CreateFontString(nil,"OVERLAY")
	  sTradeLLRR(sTradeLLFrame.titlesc,f,120,16,"地点",sTradeLLFrame.main,180,-25)
	sTradeLLFrame.titlesd = sTradeLLFrame.main:CreateFontString(nil,"OVERLAY")
	  sTradeLLRR(sTradeLLFrame.titlesd,f,110,16,"姓名",sTradeLLFrame.main,300,-25)
	sTradeLLFrame.titlese = sTradeLLFrame.main:CreateFontString(nil,"OVERLAY")
	  sTradeLLRR(sTradeLLFrame.titlese,f,120,16,"我方",sTradeLLFrame.main,410,-25)
	sTradeLLFrame.titlesf = sTradeLLFrame.main:CreateFontString(nil,"OVERLAY")
	  sTradeLLRR(sTradeLLFrame.titlesf,f,120,16,"对方",sTradeLLFrame.main,530,-25)
	sTradeLLFrame.titlesg = sTradeLLFrame.main:CreateFontString(nil,"OVERLAY")
	  sTradeLLRR(sTradeLLFrame.titlesg,f,60,16,"交易结果",sTradeLLFrame.main,650,-25)
--顶部按钮a-关闭
	sTradeLLFrame.buttona = CreateFrame("Button", nil, sTradeLLFrame.main)
	sTradeLLFrame.buttona.owner = self
	sTradeLLFrame.buttona:SetWidth(60)
	sTradeLLFrame.buttona:SetHeight(20)
	sTradeLLFrame.buttona:SetPoint("TOPRIGHT",sTradeLLFrame.main,"TOPRIGHT",-20,-5)
		t = sTradeLLFrame.buttona:CreateTexture()
	t:SetWidth(60)
	t:SetHeight(20)
	t:SetPoint("CENTER", sTradeLLFrame.buttona, "CENTER")
	t:SetTexture("Interface\\Buttons\\UI-Panel-Button-Up")
	t:SetTexCoord(0, 0.625, 0, 0.6875)
		sTradeLLFrame.buttona:SetNormalTexture(t)
	t = sTradeLLFrame.buttona:CreateTexture(nil, "BACKGROUND")
	t:SetTexture("Interface\\Buttons\\UI-Panel-Button-Down")
	t:SetTexCoord(0, 0.625, 0, 0.6875)
	t:SetAllPoints(sTradeLLFrame.buttona)
		sTradeLLFrame.buttona:SetPushedTexture(t)
	t = sTradeLLFrame.buttona:CreateTexture()
	t:SetTexture("Interface\\Buttons\\UI-Panel-Button-Highlight")
	t:SetTexCoord(0, 0.625, 0, 0.6875)
	t:SetAllPoints(sTradeLLFrame.buttona)
--	t:SetBlendMode("ADD")
		sTradeLLFrame.buttona:SetHighlightTexture(t)
	sTradeLLFrame.buttonatext = sTradeLLFrame.buttona:CreateFontString(nil,"OVERLAY")
	sTradeLLFrame.buttonatext:SetFontObject(GameFontHighlight)
	sTradeLLFrame.buttonatext:SetText("关闭")
	sTradeLLFrame.buttonatext:SetAllPoints(sTradeLLFrame.buttona)
	sTradeLLFrame.buttona:SetScript("OnClick",function() sTradeLLFrame.main:Hide();end)
--顶部按钮b-清除
	sTradeLLFrame.buttonb = CreateFrame("Button", nil, sTradeLLFrame.main)
	sTradeLLFrame.buttonb.owner = self
	sTradeLLFrame.buttonb:SetWidth(60)
	sTradeLLFrame.buttonb:SetHeight(20)
	sTradeLLFrame.buttonb:SetPoint("TOPLEFT",sTradeLLFrame.main,"TOPLEFT",20,-5)
		t = sTradeLLFrame.buttonb:CreateTexture()
	t:SetWidth(60)
	t:SetHeight(20)
	t:SetPoint("CENTER", sTradeLLFrame.buttonb, "CENTER")
	t:SetTexture("Interface\\Buttons\\UI-Panel-Button-Up")
	t:SetTexCoord(0, 0.625, 0, 0.6875)
		sTradeLLFrame.buttonb:SetNormalTexture(t)
	t = sTradeLLFrame.buttonb:CreateTexture(nil, "BACKGROUND")
	t:SetTexture("Interface\\Buttons\\UI-Panel-Button-Down")
	t:SetTexCoord(0, 0.625, 0, 0.6875)
	t:SetAllPoints(sTradeLLFrame.buttonb)
		sTradeLLFrame.buttonb:SetPushedTexture(t)
	t = sTradeLLFrame.buttonb:CreateTexture()
	t:SetTexture("Interface\\Buttons\\UI-Panel-Button-Highlight")
	t:SetTexCoord(0, 0.625, 0, 0.6875)
	t:SetAllPoints(sTradeLLFrame.buttonb)
		sTradeLLFrame.buttonb:SetHighlightTexture(t)
	sTradeLLFrame.buttonbtext = sTradeLLFrame.buttonb:CreateFontString(nil,"OVERLAY")
	sTradeLLFrame.buttonbtext:SetFontObject(GameFontHighlight)
	sTradeLLFrame.buttonbtext:SetText("清除")
	sTradeLLFrame.buttonbtext:SetAllPoints(sTradeLLFrame.buttonb)
	sTradeLLFrame.buttonb:SetScript("OnClick",function() sTradeLOG=nil;sTrade_InitializeSetup();end)
--滚动窗体
	sTradeLLFrame.sframe = CreateFrame("ScrollFrame", "sTradeLLScrollFrame", sTradeLLFrame.main, "FauxScrollFrameTemplate")
	sTradeLLFrame.sframe.owner = self
	sTradeLLFrame.sframe:SetWidth(700)
	sTradeLLFrame.sframe:SetHeight(300)
	sTradeLLFrame.sframe:SetPoint("TOPLEFT",sTradeLLFrame.main,"TOPLEFT",20,-50)
	sTradeLLFrame.sframe:SetScript("OnVerticalScroll",
	function()
		FauxScrollFrame_OnVerticalScroll(16, function() sTradeLLUpdate() end)
	end )

--在主窗体上部署要显示的字块
	sTradeLLFrame.scollsa={}
	sTradeLLFrame.scollsb={}
	sTradeLLFrame.scollsc={}
	sTradeLLFrame.scollsd={}
	sTradeLLFrame.scollse={}
	sTradeLLFrame.scollsf={}
	sTradeLLFrame.scollsg={}
	for i=1,15 do
		sTradeLLFrame.scollsa[i] = sTradeLLFrame.main:CreateFontString(nil,"ARTWORK")
		  sTradeLLRR(sTradeLLFrame.scollsa[i],f,40,16,"",sTradeLLFrame.sframe,0,-(i-1)*20-5)
		  sTradeLLFrame.scollsa[i]:SetJustifyH("CENTER")
		sTradeLLFrame.scollsb[i] = sTradeLLFrame.main:CreateFontString(nil,"ARTWORK")
		  sTradeLLRR(sTradeLLFrame.scollsb[i],f,120,16,"",sTradeLLFrame.sframe,40,-(i-1)*20-5)
		sTradeLLFrame.scollsc[i] = sTradeLLFrame.main:CreateFontString(nil,"ARTWORK")
		  sTradeLLRR(sTradeLLFrame.scollsc[i],f,120,16,"",sTradeLLFrame.sframe,160,-(i-1)*20-5)
		sTradeLLFrame.scollsd[i] = sTradeLLFrame.main:CreateFontString(nil,"ARTWORK")
		  sTradeLLRR(sTradeLLFrame.scollsd[i],f,110,16,"",sTradeLLFrame.sframe,280,-(i-1)*20-5)
		sTradeLLFrame.scollse[i] = sTradeLLFrame.main:CreateFontString(nil,"ARTWORK")
		  sTradeLLRR(sTradeLLFrame.scollse[i],f,120,16,"",sTradeLLFrame.sframe,390,-(i-1)*20-5)
		sTradeLLFrame.scollsf[i] = sTradeLLFrame.main:CreateFontString(nil,"ARTWORK")
		  sTradeLLRR(sTradeLLFrame.scollsf[i],f,120,16,"",sTradeLLFrame.sframe,510,-(i-1)*20-5)
		sTradeLLFrame.scollsg[i] = sTradeLLFrame.main:CreateFontString(nil,"ARTWORK")
		  sTradeLLRR(sTradeLLFrame.scollsg[i],f,60,16,"",sTradeLLFrame.sframe,630,-(i-1)*20-5)
	end
end

function sTrade_InitializeSetup()
--读取保存的金币交易记录，如果没有记录则新建记录用的表
if sTradeLOG == nil then sTradeLOG = {} end
if sTradeLOG[sTradeLOGa] == nil then sTradeLOG[sTradeLOGa] = {} end
if sTradeLOG[sTradeLOGb] == nil then sTradeLOG[sTradeLOGb] = {} end
if sTradeLOG[sTradeLOGc] == nil then sTradeLOG[sTradeLOGc] = {} end
if sTradeLOG[sTradeLOGd] == nil then sTradeLOG[sTradeLOGd] = {} end
if sTradeLOG[sTradeLOGe] == nil then sTradeLOG[sTradeLOGe] = {} end
if sTradeLOG[sTradeLOGf] == nil then sTradeLOG[sTradeLOGf] = {} end
if sTradeLOG[sTradeLOGg] == nil then sTradeLOG[sTradeLOGg] = {} end
	local n=table.getn(sTradeLOG[sTradeLOGa])
	sTradePlayerPay=0
	sTradeTargetPay=0
	for i=1,n do
		sTradePlayerPay = sTradePlayerPay + sTradeLOG[sTradeLOGe][i]
		sTradeTargetPay = sTradeTargetPay + sTradeLOG[sTradeLOGf][i]
	end
--更新窗体内容
	sTradeLLUpdate()
end

function sTradeLLUpdate()
	local j=table.getn(sTradeLOG[sTradeLOGa])
	FauxScrollFrame_Update(sTradeLLFrame.sframe, table.getn(sTradeLOG[sTradeLOGa]), 15, 16 );
	local sTradePos = FauxScrollFrame_GetOffset(sTradeLLFrame.sframe)
	if j<=15 then sTradePos=0;end;
	local i
	for i=1,15 do
		if j>=(i+sTradePos) then
			sTradeLLFrame.scollsa[i]:SetText(sTradeLOG[sTradeLOGa][i+sTradePos])
			sTradeLLFrame.scollsb[i]:SetText(sTradeLOG[sTradeLOGb][i+sTradePos])
			sTradeLLFrame.scollsc[i]:SetText(sTradeLOG[sTradeLOGc][i+sTradePos])
			sTradeLLFrame.scollsd[i]:SetText(sTradeLOG[sTradeLOGd][i+sTradePos])
			sTradeLLFrame.scollse[i]:SetText(C_to_GSC(sTradeLOG[sTradeLOGe][i+sTradePos]))
			sTradeLLFrame.scollsf[i]:SetText(C_to_GSC(sTradeLOG[sTradeLOGf][i+sTradePos]))
			sTradeLLFrame.scollsg[i]:SetText(sTradeLOG[sTradeLOGg][i+sTradePos])
--			sTradeLLFrame.scollsa[i]:Show()
--			sTradeLLFrame.scollsb[i]:Show()
--			sTradeLLFrame.scollsc[i]:Show()
--			sTradeLLFrame.scollsd[i]:Show()
--			sTradeLLFrame.scollse[i]:Show()
--			sTradeLLFrame.scollsf[i]:Show()
--			sTradeLLFrame.scollsg[i]:Show()
		else
			sTradeLLFrame.scollsa[i]:SetText("")
			sTradeLLFrame.scollsb[i]:SetText("")
			sTradeLLFrame.scollsc[i]:SetText("")
			sTradeLLFrame.scollsd[i]:SetText("")
			sTradeLLFrame.scollse[i]:SetText("")
			sTradeLLFrame.scollsf[i]:SetText("")
			sTradeLLFrame.scollsg[i]:SetText("")
		end
	end
	sTradeLLFrame.bottomb:SetText(C_to_GSC(sTradePlayerPay))
	sTradeLLFrame.bottomc:SetText(C_to_GSC(sTradeTargetPay))
end

function sTradeLLRR(frm,f,w,h,t,u,x,y)
--字块模板
	frm:SetFont(f,14)
	frm:SetWidth(w)
	frm:SetHeight(h)
	if t~="" then frm:SetText(t) end
	frm:ClearAllPoints()
	frm:SetJustifyH("LEFT")
	frm:SetPoint("TOPLEFT",u,"TOPLEFT",x,y)
end

function C_to_GSC(coppers)
--金银铜换算
	local g,s,c
	g=math.floor(coppers / 10000);
	s=math.floor(coppers / 100 ) - g*100;
	c=coppers - ( g * 100 + s ) * 100;
	return g..sTrade_LOC_c1..s..sTrade_LOC_c2..c..sTrade_LOC_c3;
end

function sTrade_SlashHandler(msg)
--命令行
	sTradeLLFrame.main:Show();
	sTradeLLFrame.sframe:SetVerticalScroll(34);
	sTradeLLUpdate();
end

function sTrade_NewTrade() 
--
	local trade = {
		id = nil,
		when = nil,
		where = nil,
		who = nil,
		player=UnitName("player"),
		playerMoney = 0,
		targetMoney = 0,
		playerItems = {},
		targetItems = {},
		playerItemsList = "",
		playerItemsList2 = "",
		targetItemsList = "",
		targetItemsList2 = "",
		events = {},  --for analysing cancel reason
		toofar = nil, --for further analysing cancel reason
		result = nil, --[cancelled | complete | error]
		reason = nil, --["this" | "thisrunaway" | "toofar" | "other" | "thishideui" | ERR_TRADE_BAG_FULL | ERR_TRADE_MAX_COUNT_EXCEEDED | ERR_TRADE_TARGET_BAG_FULL | ERR_TRADE_TARGET_MAX_COUNT_EXCEEDED]
	};
	return trade;
end

function sTrade_OnEvent(event, ...)
--事件触发
	local tmpWho;
--DEFAULT_CHAT_FRAME:AddMessage(event,0,0,1);
	if (event=="PLAYER_ENTERING_WORLD") then
		sTrade_InitializeSetup();
	elseif (event=="UI_ERROR_MESSAGE") then
		curr().result = sTrade_LOC_a1;
		curr().reason = arg1;
	elseif (event=="UI_INFO_MESSAGE" and ( arg1==ERR_TRADE_CANCELLED or arg1==ERR_TRADE_COMPLETE) ) then
		curr().result = (arg1==ERR_TRADE_CANCELLED) and sTrade_LOC_a2 or sTrade_LOC_a3
		sTrade_TradeAndReset();
--	elseif (event=="TRADE_CLOSED") then
--		sTrade_TradeAndReset();
	elseif (event=="TRADE_PLAYER_ITEM_CHANGED") then
		sTrade_UpdateItemInfo(arg1, "Player", curr().playerItems);
	elseif (event=="TRADE_TARGET_ITEM_CHANGED") then
		sTrade_UpdateItemInfo(arg1, "Target", curr().targetItems);
	elseif (event=="TRADE_MONEY_CHANGED") then
		sTrade_UpdateMoney();
	elseif (event=="PLAYER_TRADE_MONEY") then
--		sTrade_UpdateMoney();
	elseif (event=="TRADE_ACCEPT_UPDATE") then
--		for i = 1, 7 do
--			sTrade_UpdateItemInfo(i, "Player", curr().playerItems);
--			sTrade_UpdateItemInfo(i, "Target", curr().targetItems);
--		end
		sTrade_UpdateMoney();
	--双方之一按下交易
		if arg1==1 or arg2==1 then
			tmpWho=GetUnitName("NPC", true);
			if tmpWho ~= curr().who then
				SendChatMessage("交易对象 " .. tmpWho .. " 与预期目标 " .. curr().who .. " 不符!!! 取消交易!!!","say");
				CancelTrade();
			end;
			curr().playerItemsList = sTrade_GetTradeList("Player", curr().playerItems ,0);
			curr().playerItemsList2 = sTrade_GetTradeList("Player", curr().playerItems ,3);
			curr().targetItemsList = sTrade_GetTradeList("Target", curr().targetItems ,0);
			curr().targetItemsList2 = sTrade_GetTradeList("Target", curr().targetItems ,3);
		end;
	end
	if (event=="TRADE_REQUEST_CANCEL") then --judge the trade distance for further analysing the cancel reason
			curr().playerItemsList = sTrade_GetTradeList("Player", curr().playerItems ,0);
			curr().playerItemsList2 = sTrade_GetTradeList("Player", curr().playerItems ,3);
			curr().targetItemsList = sTrade_GetTradeList("Target", curr().targetItems ,0);
			curr().targetItemsList2 = sTrade_GetTradeList("Target", curr().targetItems ,3);
--			sTrade_UpdateMoney();
		if(UnitName("NPC")) then
			curr().toofar = CheckInteractDistance("npc", 2) and "no" or "yes"
		end
	end
	if (event=="TRADE_SHOW") then
		--curr().who=GetUnitName("NPC", true):gsub(" %- ", "-");
		curr().who=GetUnitName("NPC", true);
--		sTrade_Statue_flag=1;
	end
end

function sTrade_GetTradeList(unit,items,a)
--交易物品列表
	local list=" ";
	local funcInfo = getglobal("GetTrade"..unit.."ItemInfo");
	local funcLink = getglobal("GetTrade"..unit.."ItemLink");
	local name, texture, numItems, quality, isUsable, enchantment;
	local count=0;
	for i=1,6 do
		if(unit=="Target") then
			name, texture, numItems, quality, isUsable, enchantment = funcInfo(i);
		else
			name, texture, numItems, quality, enchantment = funcInfo(i);
		end
		if(name) then
			count=count+1;
			local itemLink = funcLink(i);

			items[i] = {
				name = name,
				numItems = numItems,
				itemLink = itemLink,
			}
			local tmp = items[i].itemLink;
			if(items[i].numItems>1) then
				tmp = tmp.."x"..items[i].numItems;
			end
			if count > a and count <= a + 3 then
				list = list .. tmp .. " ";
			end
		end
	end
	return list;
end

function sTrade_Output()
--交易结果通告
	local msg1,msg2,msg3,msg4,msg21,msg31,pm,tm;
	pm = C_to_GSC(curr().playerMoney);
	tm = C_to_GSC(curr().targetMoney);
	msg1 = curr().when .. sTrade_LOC_b1 .. curr().where .. sTrade_LOC_b2 .. curr().who .. sTrade_LOC_b3
	if curr().playerMoney == 0 then
		msg2 = "我方:" .. curr().playerItemsList;
	else
		msg2 = "我方:" .. pm .. curr().playerItemsList;
	end
	msg21 = "我方:" .. curr().playerItemsList2;
	if curr().targetMoney == 0 then
		msg3 = "对方:" .. curr().targetItemsList;
	else
		msg3 = "对方:" .. tm .. curr().targetItemsList;
	end
	msg31 = "对方:" .. curr().targetItemsList2;
	msg4 = sTrade_LOC_b4 .. curr().result
--交易未成功详情. 实际运行中堆叠物品会按堆叠数量输出多次... 还是关了吧
--	if not curr().reason == nil then msg4 = msg4 .. sTrade_LOC_b5 .. curr().reason
--	end
	if not curr().who then
		SendChatMessage(msg1,"say");
		if string.len(msg2)~=8 then SendChatMessage(msg2,"say");end;
		if string.len(msg21)~=8 then SendChatMessage(msg21,"say");end;
		if string.len(msg3)~=8 then SendChatMessage(msg3,"say");end;
		if string.len(msg31)~=8 then SendChatMessage(msg31,"say");end;
		SendChatMessage(msg4,"say");
	else
		SendChatMessage(msg1,"whisper",nil,curr().who);
		if string.len(msg2)~=8 then SendChatMessage(msg2,"whisper",nil,curr().who);end;
		if string.len(msg21)~=8 then SendChatMessage(msg21,"whisper",nil,curr().who);end;
		if string.len(msg3)~=8 then SendChatMessage(msg3,"whisper",nil,curr().who);end;
		if string.len(msg31)~=8 then SendChatMessage(msg31,"whisper",nil,curr().who);end;
		SendChatMessage(msg4,"whisper",nil,curr().who);
	end

	--如果交易中有金币发生, 写入sTradeLOG，并更新窗体内容
	if curr().playerMoney~=0 or curr().targetMoney~=0 then
		if curr().result=="交易完成" then
			sTradePlayerPay = sTradePlayerPay + curr().playerMoney
			sTradeTargetPay = sTradeTargetPay + curr().targetMoney
			curr().result = "|c0000FF00"..curr().result
		elseif curr().result=="交易取消" then
			curr().result = "|c00FF0000"..curr().result
		end
		local n=table.getn(sTradeLOG[sTradeLOGa])

		sTradeLOG[sTradeLOGa][n+1]=n+1
		sTradeLOG[sTradeLOGb][n+1]=curr().when
		sTradeLOG[sTradeLOGc][n+1]=curr().where
		sTradeLOG[sTradeLOGd][n+1]=curr().who
		sTradeLOG[sTradeLOGe][n+1]=curr().playerMoney
		sTradeLOG[sTradeLOGf][n+1]=curr().targetMoney
		sTradeLOG[sTradeLOGg][n+1]=curr().result
		sTradeLLUpdate()

		-- sTradeListFrame:NewMsg(curr().when.." "..string.format("%-15s",curr().where).." "..string.format("%-21s",curr().who).."       ",string.format("%-25s",pm),string.format("%-25s",tm),curr().result,12)
		-- sTradeListFrame:NewMsg(string.format("%-64s","　　小计:　　　　　　　　　　　　　"),string.format("%-25s",C_to_GSC(sTradePlayerPay)),string.format("%-25s",C_to_GSC(sTradeTargetPay)),"",0);
	end;
end

function sTrade_TradeAndReset()
--重置记录
	curr().when = date("%Y%m%d-%H%M%S");
	curr().where = GetRealZoneText();
	if( curr().result == sTrade_LOC_a1 ) then
		curr().reason = sTrade_CancelReason();
	elseif( curr().result == sTrade_LOC_a2 ) then
		curr().reason = curr().reason;
	end
--在交易因为距离过远, 目标死亡等意外原因中断时, 会触发UI_INFO_MESSAGE多次, 导致多次进行输出, 并且curr().who将变为空值, 然后报错.
	if curr().who ~= nil then sTrade_Output();end;
	CURRENT_sTRADE = nil;
end

function sTrade_CancelReason()
--取消原因？
	local reason = "unknown"; --should be unknown only when no trade window opened.
	local e = curr().events;
	local n = table.getn(e);
	if(n>=3 and e[n]=="TRADE_REQUEST_CANCEL" and e[n-1]=="TRADE_CLOSED" and e[n-2]=="TRADE_CLOSED") then
		reason = "self";
	elseif(n>=3 and e[n]=="TRADE_REQUEST_CANCEL" and e[n-1]=="TRADE_CLOSED" and e[n-2]=="TRADE_SHOW") then
		reason = "selfrunaway";
	elseif(n>=3 and e[n]=="TRADE_CLOSED" and e[n-1]=="TRADE_CLOSED" and e[n-2]=="TRADE_REQUEST_CANCEL") then
		if(curr().toofar == "yes") then
			reason = "toofar";
		elseif(curr().toofar == "no") then
			reason = "other";
		else
			reason = "wrong!!"; --this should not happen, if you see, contact me :p
		end
	elseif(n>=3 and e[n]=="TRADE_REQUEST_CANCEL" and e[n-1]=="TRADE_SHOW" and e[n-2]=="TRADE_CLOSED") then
		reason = "selfhideui";
	end

	curr().events = nil; --reason analyzed, abandon it to release that tiny memory
	return reason;
end

function sTrade_UpdateItemInfo(id, unit, items)
--更新交易内容
	local funcInfo = getglobal("GetTrade"..unit.."ItemInfo");
	local funcLink = getglobal("GetTrade"..unit.."ItemLink");

	local name, texture, numItems, quality, isUsable, enchantment;
	--why GetTradePlayerItemInfo and GetTradeTargetItemInfo return different things?
	if(unit=="Target") then
		name, texture, numItems, quality, isUsable, enchantment = funcInfo(id);
	else
		name, texture, numItems, quality, enchantment = funcInfo(id);
	end

	if(not name) then
		items[id] = nil;
		return;
	end

	local itemLink = funcLink(id);
	items[id] = {
		name = name,
		texture = texture,
		numItems = numItems,
		isUsable = isUsable,
		enchantment = enchantment,
		itemLink = itemLink,
	}
end

function sTrade_UpdateMoney()
--更新交易金额
	curr().playerMoney = GetPlayerTradeMoney();
	curr().targetMoney = GetTargetTradeMoney();
end

local function SendChat(msg, name)
	local channel = sTrade_FindAnnounceChannel(TradeLog_AnnounceChannel);
	if(channel=="WHISPER")then
		SendChatMessage(msg,channel,nil,name);
	else
		SendChatMessage(msg,channel);
	end
end
